<?php

namespace app\common\library;

/**
 * Client操作类
 */
class Client
{
    protected $client;
    protected $access_token;
    /**
     * 单例对象
     */
    protected static $instance;

    public function __construct($url = '')
    {
        $this->client = new \GuzzleHttp\Client([
            'base_uri' => $url ? $url : config('mp.url'),
            'timeout' => 30,
            'allow_redirects' => true
        ]);

        $this->access_token = session('access_token');
    }

    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }

        return self::$instance;
    }

    /**
     * 通用post
     *
     * @param string $url  请求地址
     * @param array $data  参数
     * @return array       返回值
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function post($url = '', $data = [], $type = 0)
    {
        $data['access_token'] = $this->access_token;
        $response = $this->client->request('post', $url, [
            'form_params' => $data
        ]);

        $content = $response->getBody();
        $content = json_decode($content, true);
        if ($type) {
            return $content;
        }
        if ($content && $content['code'] == 1) {
            return $content['data'];
        }
        return [];
    }

    /**
     * 通用json
     *
     * @param string $url 请求地址
     * @param array $data 参数
     * @return array      返回值
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function json($url = '', $data = [])
    {
        $response = $this->client->request('post', $url, [
            'json' => $data
        ]);
        $content = $response->getBody();
        $content = json_decode($content, true);
        if ($content && $content['code'] == 1) {
            return $content['data'];
        }
        return [];
    }

    /**
     * 学校列表
     *
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function schools()
    {
        return self::post('/admin/api/school/lists');
    }

    public function schoolProfile($ids)
    {
        return self::post('/admin/api/school/profile', ['ids' => $ids]);
    }

    public function schoolDetail($id)
    {
        return self::post('/admin/api/school/detail', ['id' => $id]);
    }

    /**
     * 根据学校id获取园区列表
     *
     * @param int $school_id 学校id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function gardens($school_id)
    {
        return self::post('/admin/api/school/gardens', ['id' => $school_id]);
    }

    /**
     * 根据园区id获取楼栋列表
     *
     * @param int $garden_id 园区id
     * @param int $type 类型
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildings($garden_id, $type = 2)
    {
        return self::post('/admin/api/garden/buildings', ['id' => $garden_id, 'type' => $type]);
    }

    /**
     * 根据学校id、园区number 获取楼栋列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/23
     *
     * @param $school_id
     * @param $garden_number
     * @param int $type
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildingsByNumber($school_id, $garden_number, $type = 2)
    {
        return self::post('/admin/api/garden/buildingsByNumber', ['school_id' => $school_id, 'garden_number' => $garden_number, 'type' => $type]);
    }

    /**
     * 获取楼栋信息
     *
     * @param int $building_id 楼栋id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function building($building_id)
    {
        return self::post('/admin/api/building/index', ['id' => $building_id]);
    }

    /**
     * 获取楼栋信息
     *
     * @param int $building_id 楼栋id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildingProfile($building_id)
    {
        return self::post('/admin/api/building/profile', ['id' => $building_id]);
    }

    /**
     * 楼栋列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 15:38
     *
     * @param $where
     * @param $sort
     * @param $order
     * @param $offset
     * @param $limit
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildingList($where, $sort, $order, $offset, $limit, $type = 2)
    {
        return self::post('/admin/api/building/lists', compact('where', 'sort', 'order', 'offset', 'limit', 'type'));
    }

    public function buildingDetail($school_id, $garden_number, $building_number)
    {
        return self::post('/admin/api/building/detail', compact('school_id', 'garden_number', 'building_number'));
    }

    /**
     * 年级列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 15:54
     *
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function grades()
    {
        return self::post('/admin/api/grade', []);
    }

    /**
     * 班级列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 15:56
     *
     * @param $school_id
     * @param $grade_id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function classes($school_id, $grade_id)
    {
        return self::post('/admin/api/grade/classes', compact('school_id', 'grade_id'));
    }

    public function classesProfile($class_ids)
    {
        return self::post('/admin/api/classes/profiles', ['ids' => $class_ids]);
    }

    public function students($where, $sort, $order, $offset, $limit)
    {
        return self::post('/admin/api/student/lists', compact('where', 'sort', 'order', 'offset', 'limit'));
    }

    public function student($student_id)
    {
        return self::post('/admin/api/student/index', ['id' => $student_id]);
    }

    public function studentProfile($student_ids)
    {
        return self::post('/admin/api/student/profiles', ['ids' => $student_ids]);
    }

    public function studentsGroupBySchool()
    {
        return self::post('/admin/api/student/studentsGroupBySchool', []);
    }

    public function schoolStudents()
    {
        return self::post('/admin/api/school/students', []);
    }

    public function schoolStudentCount($school_id, $gender, $is_bed)
    {
        return self::post('/admin/api/school/studentCount', compact('school_id', 'gender', 'is_bed'));
    }

    public function studentNoBedList($school_id, $gender)
    {
        return self::post('/admin/api/school/studentNoBedList', compact('school_id', 'gender'));
    }

    public function schoolStudentBeds()
    {
        return self::post('/admin/api/school/studentBeds', []);
    }

    public function setStudentBed($student_ids)
    {
        if (!is_array($student_ids)) {
            $student_ids[] = $student_ids;
        }
        return self::post('/admin/api/student/setBed', ['ids' => $student_ids]);
    }

    public function teacher($id)
    {
        return self::post('/admin/api/teacher/index', ['id' => $id]);
    }

    public function teacherList($where, $sort, $order, $offset, $limit)
    {
        return self::post('/admin/api/teacher/lists', compact('where', 'sort', 'order', 'offset', 'limit'));
    }

    public function groups()
    {
        return self::post('/admin/api/admin/groups');
    }

    public function group($id)
    {
        return self::post('/admin/api/admin/group', ['id' => $id]);
    }

    public function groupProfile($ids)
    {
        return self::post('/admin/api/admin/groupProfile', ['ids' => $ids]);
    }

    public function authGroup($ids)
    {
        return self::post('/admin/api/admin/authGroup', ['ids' => $ids]);
    }

    public function userGroups($id)
    {
        return self::post('/admin/api/admin/userGroups', ['id' => $id]);
    }

    public function userGroupAccess($uid)
    {
        return self::post('/admin/api/admin/userGroupAccess', ['uid' => $uid]);
    }

    public function userProfile($ids)
    {
        return self::post('/admin/api/user/profile', ['id' => $ids]);
    }

    public function userList($data)
    {
        return self::post('/admin/api/user/lists', $data);
    }

    public function dormProfile($ids)
    {
        return self::post('/admin/api/dorm/getProfileByIds', ['ids' => $ids]);
    }
}
